package com.pctx.rbac.service.impl;

import com.pctx.common.util.DateTimeUtil;
import com.pctx.common.util.UuidUtil;
import com.pctx.rbac.dao.RoleMapper;
import com.pctx.rbac.pojo.Role;
import com.pctx.rbac.pojo.RoleExample;
import com.pctx.rbac.service.PermissionService;
import com.pctx.rbac.service.RolePermissionService;
import com.pctx.rbac.service.RoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by chason on 2018/2/26.9:39
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RolePermissionService rolePermissionService;

    @Override
    public List<Role> getByCompanyUuid(String companyUuid) throws Exception{
        if (StringUtils.isEmpty(companyUuid))
            return null;

        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyUuidEqualTo(companyUuid).andStatusNotEqualTo(Role.STATUS_DEL);

        return roleMapper.selectByExample(example);
    }

    @Override
    public Role getByUuid(String uuid) throws Exception {
        if (StringUtils.isEmpty(uuid))
            return null;

        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andUuidEqualTo(uuid).andStatusNotEqualTo(Role.STATUS_DEL);

        List<Role> list = roleMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list))
            return null;

        Role role = list.get(0);
        role.setPermissions(permissionService.getByRoleUuid(uuid));

        return role;
    }

    @Override
    public boolean add(Role role) throws Exception {
        if (null == role)
            return false;

        role.setUuid(UuidUtil.randomUUID());
        role.setCreatedTime(DateTimeUtil.nowDate());
        role.setUpdateTime(DateTimeUtil.nowDate());
        role.setStatus(Role.STATUS_OK);

        boolean flag = roleMapper.insertSelective(role) > 0;

        if (flag)
            rolePermissionService.addRolePermissions(role.getUuid(), role.getPermissionUuids());

        return flag;
    }

    @Override
    public boolean update(Role role) throws Exception {
        if (null == role || StringUtils.isEmpty(role.getUuid()))
            return false;

        Role old = getByUuid(role.getUuid());
        if (null == old)
            return false;

        role.setUpdateTime(DateTimeUtil.nowDate());
        role.setId(old.getId());
        boolean flag = roleMapper.updateByPrimaryKeySelective(role) > 0;

        if (flag) {
            // 删除以前的权限
            rolePermissionService.delByRoleUuid(role.getUuid());
            // 添加新的权限
            rolePermissionService.addRolePermissions(role.getUuid(), role.getPermissionUuids());
        }

        return flag;
    }

    @Override
    public boolean delete(String uuid) throws Exception {
        if (StringUtils.isEmpty(uuid))
            return false;

        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andUuidEqualTo(uuid);

        boolean flag = roleMapper.deleteByExample(example) > 0;

        if (flag)
            rolePermissionService.delByRoleUuid(uuid);

        return flag;
    }
}
